package com.credithc.signature.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.io.ClassPathResource;
import tk.mybatis.spring.annotation.MapperScan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉24⽇<br/>
 * @version：v1.0.0<br>
 */
@SpringBootApplication(scanBasePackages = {"com.credithc.signature"})
@EnableFeignClients("com.credithc")
@MapperScan("com.credithc.signature.common.mapper")
@EnableEurekaClient
@Slf4j
@ServletComponentScan("com.fadada.servlet")
@EnableRabbit
public class SignatureJobApplication {

    private static Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
    private static LocalDateTime dateTime = LocalDateTime.now();

    public static void main(String[] args) {
        SpringApplication.run(SignatureJobApplication.class, args);
        String envConfig = getEnvConfig();
        log.info("当前法大大配置文件使用的环境为:【{}】", envConfig);
        log.info("SignatureJobApplication start ok ......");
        log.info("当前Job版本号:【signature-{}】", dateTimeFormatter.format(dateTime));
    }

    /**
     * 获取法大大配置文件使用环境
     *
     * @return
     */
    public static String getEnvConfig() {
        ClassPathResource classPathResource = new ClassPathResource("fadada-config.properties");
        InputStream inputStream = null;
        try {
            inputStream = classPathResource.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String str = reader.readLine();
            Matcher matcher = pattern.matcher(str);
            StringBuilder builder = new StringBuilder();
            while (matcher.find()) {
                String matchStr = matcher.group(0);
                builder.append(matchStr);
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
